package cn.skylark.wcm.common.splitter.doc;

import cn.skylark.wcm.common.util.Constants;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class TxtSplitter {
    /**
     *  doc文件的txt拆分
     * @param filename 文件路径
     * @param outpath  输出路径
     * @param remotepath 云服务器远端路径
     * @param splitpara 拆分的数组
     * @throws IOException
     */
    public static void txtSplitter( String filename, String outpath, String[] remotepath, Integer[] splitpara ) throws IOException {
        File file = new File(filename);
        //1.如果是doc文档
        InputStream is = new FileInputStream(filename);
        HWPFDocument doc = new HWPFDocument(is);
        Range range = doc.getRange();
        File folder = new File(outpath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        Paragraph para;
        //2.转html和txt
        for (int j = 0; j < splitpara.length - 1; j++) {
            //System.out.println("段落长度" + splitpara.length);
            Paragraph paragraph = range.getParagraph(splitpara[j]);
            String name = (paragraph.text()).trim();
            String oldfile;
            if (name.contains(" ")){
                oldfile =  name.substring(name.lastIndexOf(" ")+1);
            }else{
                oldfile = name;
            }
            oldfile= oldfile.replaceAll("/","-");
            if (j != splitpara.length - 1) {
                File file1 = new File(outpath + File.separator + oldfile);
                if (file1.exists()) {
                    file1.delete();
                }
                String newRemotepath = "";
                String fileName = "";
                for (int i = 0; i < remotepath.length; i++) {
                    fileName = remotepath[i].substring(remotepath[i].lastIndexOf(">") + 1);
                    //System.out.println("filename===" + fileName);
                    if (fileName.equals(oldfile)) {
                        //http://publictest-1253363526.file.myqcloud.com/file/17813e16ded146a9872da22c246917b9.html>
                        newRemotepath = remotepath[i].substring(remotepath[i].lastIndexOf("/") + 1, remotepath[i].lastIndexOf(">"));
                    }
                }
                //System.out.println(newRemotepath);
                http://publictest-1253363526.file.myqcloud.com/html/fe4befaf908b480683cb46ed8acf48c5.html
                //防止出现下表越界的情况
                Util.writeFile(name.trim() + "\r\n" + Constants.SERVERPATH+"/"+"upload/html/"+ newRemotepath, outpath + File.separator + oldfile+".txt", true);
                for (int k = splitpara[j]; k < splitpara[j + 1]; k++) {//段落数开始遍历
                    para = range.getParagraph(k);//获取这一段的属性
                    //按段输出文本
                    String text = para.text().trim();
                    Util.writeFile("\r\n" + text, outpath + File.separator + oldfile+".txt", true);
                }//段落便利结束
            }
        }
    }

}


